/**
 * @file util/ping.c
 * @brief ping dynamic loadable library
 * @author Bojan Nesovic
 * @date 2013-11-14
*/
#include <stdio.h>
#include <string.h>
#include "ping.h"

int ping(char *host, FILE *outputfile, int count, int s) {
  FILE *pptr = NULL;
  char systemcall[1280];
  char buffer[1280];

  if(s==0){
    snprintf(systemcall, sizeof(systemcall), "ping %s -c %d", host, count);
  }else{
    snprintf(systemcall, sizeof(systemcall), "ping %s -c %d |grep -v rtt", host, count);
  }
  
  pptr = popen(systemcall, "r");

  if (pptr == NULL)
    return -1;

  printf("ping %s\n", host);

  /* read lines from process ping  */
  while (fgets(buffer, 1023, pptr) != NULL) {
    fwrite(buffer, strlen(buffer), 1, outputfile);
  }

  pclose(pptr);

  return 0;
}
